﻿Imports System.Data
Imports System.Data.SqlClient

Public Class ProvLojaDAO
    Implements ILoja

    Private connection As SqlConnection

    Public Sub New(ByVal connection As SqlConnection)
        Me.connection = connection
    End Sub

    Public Function AtualizarLoja(ByVal parIdLoja As Integer, ByVal parNovaLoja As SuporteModel.Loja) As Boolean Implements ILoja.AtualizarLoja

    End Function

    Public Function BuscarLojaPorCliente(ByVal parIdCliente As Integer) As System.Collections.Generic.List(Of SuporteModel.Loja) Implements ILoja.BuscarLojaPorCliente

    End Function

    Public Function CadastrarLoja(ByVal parLoja As SuporteModel.Loja) As Boolean Implements ILoja.CadastrarLoja

    End Function

    Public Function ExcluirLoja(ByVal parIdLoja As Integer) As Boolean Implements ILoja.ExcluirLoja

    End Function

    Public Function ListarLojas() As System.Collections.Generic.List(Of SuporteModel.Loja) Implements ILoja.ListarLojas

        Dim listaLojas As New List(Of SuporteModel.Loja)
        Try
            Me.connection.Open()
            Dim comm As New SqlCommand("select LOJ_NOME,LOJ_CODIGO,LOJ_OBS,LOJ_EMAIL,LOJ_GERENTE,LOJ_TELEFONE,LOJ_CLIENTE from LOJAS", Me.connection)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()

            While dataReader.Read()
                Dim loja As New SuporteModel.Loja
                loja.Codigo = Convert.ToInt64(dataReader("LOJ_CODIGO"))
                loja.Cliente = New SuporteModel.Cliente
                loja.Cliente.Codigo = Convert.ToInt64(dataReader("LOJ_CLIENTE"))
                loja.Nome = dataReader("LOJ_NOME").ToString()
                loja.Observacao = dataReader("LOJ_OBS").ToString()
                loja.Email = dataReader("LOJ_EMAIL").ToString()
                loja.Gerente = dataReader("LOJ_GERENTE").ToString()
                loja.Telefone = dataReader("LOJ_TELEFONE").ToString()
                listaLojas.Add(loja)
            End While

        Catch ex As Exception

        Finally
            Me.connection.Close()
        End Try
        Return listaLojas
    End Function

    Public Function BuscarLoja(parIdLoja As Integer) As SuporteModel.Loja Implements ILoja.BuscarLoja
        Dim loja As New SuporteModel.Loja
        Try
            Me.connection.Open()
            Dim comm As New SqlCommand("select LOJ_NOME,LOJ_CODIGO,LOJ_OBS,LOJ_EMAIL,LOJ_GERENTE,LOJ_TELEFONE,LOJ_CLIENTE from LOJAS WHERE LOJ_CODIGO = @codigo", Me.connection)
            comm.Parameters.AddWithValue("@codigo", parIdLoja)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()
            If dataReader.Read() Then
                loja.Codigo = Convert.ToInt64(dataReader("LOJ_CODIGO"))
                loja.Cliente = New SuporteModel.Cliente
                loja.Cliente.Codigo = Convert.ToInt64(dataReader("LOJ_CLIENTE"))
                loja.Nome = dataReader("LOJ_NOME").ToString()
                loja.Observacao = dataReader("LOJ_OBS").ToString()
                loja.Email = dataReader("LOJ_EMAIL").ToString()
                loja.Gerente = dataReader("LOJ_GERENTE").ToString()
                loja.Telefone = dataReader("LOJ_TELEFONE").ToString()
            End If
        Catch ex As Exception
        Finally
            Me.connection.Close()
        End Try
        Return loja
    End Function
End Class
